package cn.zhou.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: zhou
 * @Date: Create in 9:42 2020/7/22
 */
@Service
public class PaymentService {

    //正常访问，肯定OK的方法
    public String paymentInfo_OK(Integer id){
        return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_OK,  id:  "+id
                +"\t"+"O(∩_∩)O哈哈~";
    }

//    //模拟拥堵的情况
//    public String paymentInfo_Timeout(Integer id){
//
//        int timeNumber = 3;
//
//        try {
//            TimeUnit.SECONDS.sleep(timeNumber);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_Timeout,  id:  "+id
//                +"\t"+"╭(╯^╰)╮" + "耗时"+timeNumber+"秒钟";
//    }
    //模拟拥堵的情况               业务类上启动@HystrixCommand
    /**
     * 规定这个线程的超时时间是5s，5s后就由fallbackMethod指定的方法帮我“兜底”（服务降级）
     * 联合feign_order 此时超过五秒选择兜底 线程沉睡3秒可以执行 在feign客户端设置超1.5秒选择客户端兜底方法
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler", commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",
                    value = "5000")
    })
    public String paymentInfo_Timeout(Integer id){
        //测试超时报错
         int timeNumber = 3;
                 try {
                     TimeUnit.SECONDS.sleep(timeNumber);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }

         return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_Timeout,  id:  "+id
               +"\t"+"╭(╯^╰)╮" + "耗时"+timeNumber+"秒钟";


        /***          测试异常报错
         * int age = 10/0;
         *
         *         return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_Timeout,  id:  "+id
         *                 +"\t"+"╭(╯^╰)╮";
         */


    }

    public String paymentInfo_TimeoutHandler(Integer id){
        return "线程池：  "+ Thread.currentThread().getName()+"   系统繁忙或运行报错,请稍后再试,  id:  "+id
                +"\t"+"o(╥﹏╥)o";
    }

    //==================服务熔断
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否开启断路器

            ////当在配置时间窗口内达到此数量的失败后，进行断路。10个/10s 注意分母是10s。 默认20个/10s
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            //断路多久以后开始尝试是否恢复，默认5s
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            //失败率达到多少后跳闸
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        if(id < 0){
            throw new RuntimeException("******id 不能为负数");
        }
        String serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName()+"\t"+"调用成功，流水号： "+serialNumber;
    }
    //服务降级方法
    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
        return "id 不能为负数，请稍后再试， o(╥﹏╥)o  id: "+id;
    }

}

